home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 June / SGI Freeware 1998 June.iso / dist / fw_UMINNgopher.idb / usr / freeware / src / gopher_1.12 / gopherd / Waisindex.c.z / Waisindex.c
C/C++ Source or Header  |  1997-09-09  |  11KB  |  425 lines

  1. /********************************************************************
  2.  * $Author: drich $
  3.  * $Revision: 1.1 $
  4.  * $Date: 1995/10/03 04:08:22 $
  5.  * $Source: /proj/freeware1.0/gopher1.12/src/gopherd/RCS/Waisindex.c,v $
  6.  * $State: Exp $
  7.  *
  8.  * Paul Lindner, University of Minnesota CIS.
  9.  *
  10.  * Copyright 1991, 1992 by the Regents of the University of Minnesota
  11.  * see the file "Copyright" in the distribution for conditions of use.
  12.  *********************************************************************
  13.  * MODULE: Waisindex.c
  14.  * Routines to translate wais indexes on disk to gopher
  15.  *********************************************************************
  16.  * Revision History:
  17.  * $Log: Waisindex.c,v $
  18.  * Revision 1.1  1995/10/03  04:08:22  drich
  19.  * gopher 1.2 check-in
  20.  *
  21.  * Revision 1.4  1993/01/05  02:41:28  lindner
  22.  * .cap files are now ignored by the indexer
  23.  *
  24.  * Revision 1.3  1993/01/01  00:12:41  lindner
  25.  * Fixed parameters to GDnew()
  26.  *
  27.  * Revision 1.2  1992/12/21  20:36:44  lindner
  28.  * Added #include for cutil.h (from dgg)
  29.  *
  30.  * Revision 1.1  1992/12/10  23:13:27  lindner
  31.  * gopher 1.1 release
  32.  *
  33.  *
  34.  *********************************************************************/
  35.  
  36. #if defined(WAISSEARCH)
  37.  
  38. /* WIDE AREA INFORMATION SERVER SOFTWARE
  39.    No guarantees or restrictions.  See the readme file for the full standard
  40.    disclaimer.    
  41.    Brewster@think.com
  42.  
  43.    Heavily hacked by Paul Lindner (lindner@boombox.micro.umn.edu)
  44.    Do you even recognize this Brewster? :-)
  45.  
  46. */
  47.  
  48. int ShowDate = 0;
  49.  
  50. #define _search_c
  51.  
  52. #include "gopherd.h"
  53.  
  54.  
  55. #if defined(_AIX)
  56. #define ANSI_LIKE
  57. #endif
  58.  
  59. #include "../ir/irext.h"
  60. #include "../ir/irsearch.h"
  61. #include "../ir/docid.h"
  62. #include "../ir/irtfiles.h"
  63. #include "../ir/cutil.h"    /** fix for -DBIO wais needs.. **/
  64. #include <math.h>
  65.  
  66.  
  67. FILE *logfile = NULL; /* the logfile */
  68. char *log_file_name = NULL;
  69.  
  70. static char *DefaultDB = "index";
  71. static char *MonthStr[] = {
  72.      "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Sept", "Oct",
  73.       "Nov", "Dec"
  74. };
  75.  
  76. #if defined(void)
  77. #undef void
  78. #endif
  79.  
  80.  
  81.  
  82. int
  83. Process_Veronica(besthit, gs)
  84.   hit *besthit;
  85.   GopherObj *gs;
  86. {
  87.      FILE *ZeFile;
  88.      char veronicabuf[1024];
  89.      char *data, *cp;
  90.  
  91.      /*** Open up the file and seek to the right position ***/
  92.  
  93.      ZeFile = ufopen(besthit->filename, "r");
  94.  
  95.      if (ZeFile == NULL)
  96.       return(-1);
  97.  
  98.      fseek(ZeFile, besthit->start_character, 0);
  99.  
  100.      bzero(veronicabuf, sizeof(veronicabuf));
  101.      fread(veronicabuf, 1, besthit->end_character - besthit->start_character,
  102.        ZeFile);
  103.      veronicabuf[besthit->end_character - besthit->start_character+1] = '\0';
  104.      
  105.      
  106.      data = veronicabuf;
  107.      GSsetType(gs, *data);
  108.      
  109.      ZapCRLF(data);
  110.      
  111.      cp = strchr(data, '\t');
  112.      *cp = '\0';
  113.      GSsetTitle(gs, data+1);
  114.      
  115.      data = cp+1;
  116.      cp = strchr(data, '\t');
  117.      *cp = '\0';
  118.      GSsetPath(gs, data);
  119.      
  120.      data = cp + 1;
  121.      cp = strchr(data, '\t');
  122.      *cp = '\0';
  123.      GSsetHost(gs, data);
  124.      
  125.      GSsetPort(gs, atoi(cp+1));
  126.  
  127.      fclose(ZeFile);
  128.      return(0);
  129. }
  130.  
  131. void
  132. WaisIndexQuery(sockfd, index_directory, SearchWords, new_db_name, INDEXHost, INDEXPort, INDEXPath)
  133.   int sockfd;
  134.   char *index_directory;
  135.   char *SearchWords;
  136.   char *new_db_name;
  137.   char *INDEXHost;
  138.   int  INDEXPort;
  139.   char *INDEXPath;
  140.      database* db;
  141.      long maxRawScore;
  142.      long normalScore;
  143.      char *cp;
  144.      char *Selstrout;
  145.      char dateline[10];
  146.      long i;
  147.      query_parameter_type parameters;
  148.      boolean search_result;
  149.      char score[6];
  150.      static char ReturnLine[512];
  151.                                         
  152.      char * sidename;                    /* mtm 11-23-92 */
  153.      FILE * SideFile = NULL;             /* mtm 11-23-92 */
  154.  
  155.      GopherDirObj *gd;
  156.      GopherObj    *gs;
  157.      
  158.  
  159.      gs = GSnew();
  160.      gd = GDnew(32);
  161.  
  162.      if (DEBUG)  {
  163.       fprintf(stderr, "IndexPath: %s\n", INDEXPath);
  164.       logfile = stderr;   /** Log wais error messages to console **/
  165.      }     else {
  166.       logfile = ufopen("/dev/null", "w+");
  167.      }
  168.  
  169.      if (new_db_name == NULL) {
  170.       new_db_name = DefaultDB;
  171.      }
  172.  
  173.      if (uchdir(index_directory)) {
  174.       Abortoutput(sockfd, "Couldn't change to index directory...");
  175.       return;
  176.      }
  177.  
  178.      if (SearchWords != NULL && strlen(SearchWords) == 0) {
  179.       EveryWAISdocument(new_db_name);
  180.       return;
  181.      }
  182.  
  183.      db = openDatabase(new_db_name, false, true);
  184.      
  185.      if (db == NULL) {
  186.       sprintf(ReturnLine, "Failed to open database %s in index dir %s", new_db_name, index_directory);
  187.       Abortoutput(sockfd, ReturnLine);
  188.       writestring(sockfd, ".\r\n"); /** be polite **/
  189.       return;
  190.      }
  191.      
  192. #ifdef BIO            /* dgg */
  193. {
  194.      char *cp= read_delimiters( db);  /* use data-specific delim, available */
  195.  
  196.      if (cp != NULL) {
  197.       strcpy( gDelimiters, cp);
  198.       wordDelimiter= wordbreak_user;
  199.      }
  200.      else
  201.       wordDelimiter= wordbreak_notalnum;
  202. }
  203. #endif
  204.  
  205.      parameters.max_hit_retrieved = 256;
  206.  
  207.      set_query_parameter(SET_MAX_RETRIEVED_MASK, ¶meters);
  208.      
  209.      search_result = false;
  210.      search_result |= search_for_words(SearchWords, db, 0);
  211.      
  212.      if (search_result == true) {
  213.       /* the search went ok */
  214.       hit best_hit;
  215.       
  216.       finished_search_word(db);
  217.       if (DEBUG)
  218.            printf("After finished_search\n");
  219.  
  220.       uchdir(Data_Dir); /* necessary to find side files */
  221.  
  222.       for (i = 0; i < parameters.max_hit_retrieved; i++){ 
  223.            if (0 != next_best_hit(&best_hit, db))
  224.             break;        /* out of hits */
  225.            if (i == 0)
  226.             maxRawScore = best_hit.weight;
  227.            if (best_hit.weight > 0 && 
  228.            strstr(best_hit.filename, ".cache")==NULL &&
  229.            strstr(best_hit.filename, ".cap/")==NULL){
  230.             long lines,length;
  231.  
  232.             char** type = NULL;
  233.             
  234.             normalScore = (long)floor((((double)best_hit.weight) /
  235.                            ((double)maxRawScore)) *    
  236.                           (MAX_NORMAL_SCORE + 1));
  237.  
  238.             if (normalScore > MAX_NORMAL_SCORE)
  239.              normalScore = MAX_NORMAL_SCORE;
  240.             
  241.  
  242.             /*** Strip off the first part of the path in the filename*/
  243.             /*** Plus it gets rid of weird automount things... ***/
  244.             Selstrout =strstr(best_hit.filename, INDEXPath);
  245.             if (Selstrout == NULL)
  246.              Selstrout = "Error in Hostdata!";
  247.             else
  248.              Selstrout += strlen(INDEXPath);
  249.             
  250.  
  251.                     sprintf(score,"%3d ",best_hit.weight);
  252.  
  253.                     waislog(0,99,"%s: Score %3d:%s",SearchWords,best_hit.weight,Selstrout);
  254.             
  255.             /** Make the outgoing string **/
  256.  
  257.             ZapCRLF(best_hit.headline);
  258.             
  259.             /*** Remove the gopher data directory pathname if
  260.                  it's there from the headline
  261.             ***/
  262.  
  263.             if ((cp = strstr(best_hit.headline, INDEXPath)) != NULL) {
  264.              /*** Dangerous.... ***/
  265.              strcpy(cp, cp+strlen(INDEXPath));
  266.             } 
  267.              
  268.             GSsetType(gs, '0');
  269.             GSsetTitle(gs, best_hit.headline);
  270.             GSsetHost(gs, INDEXHost);
  271.             GSsetPort(gs, INDEXPort);
  272.  
  273.                  /* removed "/" from following line (before %s) . 
  274.                 Was getting double slash at least with w8b5bio; 
  275.                 mtm 11-23-92 */
  276.  
  277.             sprintf(ReturnLine, "R%d-%d-%s",
  278.                 best_hit.start_character, best_hit.end_character,
  279.                 Selstrout);
  280.             
  281.             if (!MacIndex)
  282.             GSsetPath(gs, ReturnLine);
  283.             else
  284.             GSsetPath(gs, Selstrout);
  285.             GSsetWeight(gs, best_hit.weight);
  286.             
  287.                     /* 
  288.              * Find and process sidefile. 
  289.              * Allow worst case name length. 
  290.              */
  291.  
  292.             if((sidename = (char *) malloc((unsigned) 
  293.                 strlen(Selstrout) + 
  294.                     strlen("/.cap/") + 1)) != NULL) {
  295.               if((cp = mtm_basename(Selstrout)) != Selstrout) {
  296.             /*  turn "/foo/bar/baz" into "/foo/bar/.cap/baz" */
  297.             strncpy(sidename,Selstrout,(cp - Selstrout));
  298.             *(sidename + (cp - Selstrout)) = '\0';
  299.             strcat(sidename,".cap/");
  300.             strcat(sidename,cp);
  301.               }
  302.               else {
  303.               /* root of the gopher tree, this is easier... */
  304.             strcpy(sidename,"/.cap/");
  305.             strcat(sidename,Selstrout);
  306.               }
  307.               if ((SideFile = rfopen(sidename, "r")) != NULL) {
  308.             if (DEBUG == TRUE)
  309.               printf("Side file name: %s\n", sidename);
  310.             Process_Side(SideFile, gs);
  311.               }
  312.               free(sidename);
  313.             }
  314.             
  315.             if (DEBUG) printf("Doc type is %s\n", best_hit.type);
  316.             if (strcmp(best_hit.type, "GOPHER")==0) {
  317.             if (DEBUG) printf("Got a veronica style thing %s\n",best_hit.headline);
  318.             Process_Veronica(&best_hit, gs);
  319.            }            
  320.             
  321.             GStoNet(gs,sockfd);
  322.  
  323.            }
  324.            
  325.            
  326.            if (DEBUG) {
  327.             printf("%s\n", ReturnLine);
  328.             printf("End Byte   = %d\n", best_hit.end_character);
  329.             printf("Doc length = %d\n", best_hit.document_length);
  330.             printf("#lines     = %d\n", best_hit.number_of_lines);
  331.            }
  332.       }
  333.      }
  334.      else {
  335.       /* something went awry in the search */
  336.       LOGGopher(sockfd, "Something went wrong in the search!\r\n");
  337.       writestring(sockfd, ".\r\n"); /*** be polite, don't screw up the client**/
  338.       return;
  339.      }
  340.      finished_best_hit(db);
  341.  
  342.      writestring(sockfd, ".\r\n");
  343.  
  344.      /* free everything */
  345.      closeDatabase(db);
  346.      return;
  347. }
  348.  
  349. EveryWAISdocument(sockfd, db, INDEXHost, INDEXPort, INDEXPath)
  350.   int sockfd;
  351.   char *db;
  352.   char *INDEXHost;
  353.   int  INDEXPort;
  354.   char *INDEXPath;
  355. {
  356.      FILE         *dbcatalog;
  357.      char         db_name[MAXPATHLEN];
  358.      char         inputline[512];
  359.      String       *Headline;
  360.      String       *Filename;
  361.      int          StartByte, EndByte;
  362.      GopherObj    *gs;
  363.      GopherDirObj *gd;
  364.      boolean      Headlineset = FALSE;
  365.      boolean      DocIDset    = FALSE;
  366.  
  367.      gs = GSnew();
  368.      gd = GDnew(32);
  369.      Headline = STRnew();
  370.      Filename = STRnew();
  371.  
  372.      strcpy(db_name, db);
  373.      strcat(db_name, ".cat");
  374.  
  375.      dbcatalog = rfopen(db_name, "r");
  376.      
  377.      while (fgets(inputline, sizeof(inputline), dbcatalog) != NULL) {
  378.       if (strncmp(inputline, "Headline: ", 10)==0) {
  379.            STRset(Headline, inputline +10);
  380.            Headlineset = TRUE;
  381.       }
  382.       else if (strncmp(inputline, "DocID: ", 7)==0) {
  383.            char *cp;
  384.  
  385.            StartByte = atoi(inputline);
  386.            cp = strchr(inputline+7, ' ');
  387.            if (cp == NULL) break;
  388.  
  389.            cp++;
  390.            EndByte = atoi(cp);
  391.  
  392.            cp = strchr(inputline+7, ' ');
  393.            cp++;
  394.            if (cp == NULL) break;
  395.  
  396.            cp =strstr(cp, INDEXPath);
  397.            if (cp == NULL) break;
  398.            
  399.            STRset(Filename, cp);
  400.  
  401.            DocIDset = TRUE;
  402.       }
  403.       
  404.       if (DocIDset == TRUE && Headlineset == TRUE) {
  405.            char tmppath[512];
  406.  
  407.            sprintf(tmppath, "R%d-%d-%s", StartByte, EndByte, STRget(Filename));
  408.  
  409.            GSsetType(gs, '0');
  410.            GSsetTitle(gs, STRget(Headline));
  411.            GSsetHost(gs, INDEXHost);
  412.            GSsetPort(gs, INDEXPort);
  413.            GSsetPath(gs, tmppath);
  414.  
  415.            GDaddGS(gd, gs);
  416.  
  417.            DocIDset = FALSE;
  418.            Headlineset = FALSE;
  419.       }
  420.      }      
  421. }
  422.  
  423. #endif /** WAISSEARCH **/
  424.